package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author 虎哥
 */
@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 分页查询品牌
     *
     * @param page   当前页
     * @param rows   每页大小
     * @param key    搜索关键字
     * @param sortBy 排序字段
     * @param desc   是否降序
     */
    public PageResult<BrandDTO> queryBrandByPage(Integer page, Integer rows, String key, String sortBy, Boolean desc) {
        // 0.分页
        PageHelper.startPage(page, rows);
        // 1.构建查询条件
        Example example = new Example(Brand.class);
        // 1.1.搜索关键字
        if (StringUtils.isNotBlank(key)) {
            example.createCriteria().orLike("name", "%" + key + "%")
                    .orEqualTo("letter", key);
        }
        // 1.2.排序条件
        String orderByClause = sortBy + (desc ? " DESC" : " ASC");
        example.setOrderByClause(orderByClause);

        // 2.查询
        List<Brand> list = brandMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        // 3.解析
        // 3.1.DTO转换
        List<BrandDTO> dtoList = BeanHelper.copyWithCollection(list, BrandDTO.class);
        // 3.2.分页信息
        PageInfo<Brand> info = new PageInfo<>(list);

        return new PageResult<>(info.getTotal(), info.getPages(), dtoList);
    }

    @Transactional
    public void saveBrand(BrandDTO brandDTO, List<Long> cids) {
        // 把DTO转为PO
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        // 新增品牌, insertSelective只对对象中的非空字段完成新增
        int count = brandMapper.insertSelective(brand);
        if (count != 1) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }

        // 新增中间表
        count = brandMapper.insertCategoryBrand(cids, brand.getId());
        if (count != cids.size()) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    public BrandDTO queryById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand == null) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand, BrandDTO.class);
    }

    public List<BrandDTO> queryBrandOfCategory(Long cid) {
        List<Brand> list = brandMapper.queryBrandByCategoryId(cid);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, BrandDTO.class);
    }

    public List<BrandDTO> queryByIds(List<Long> idList) {
        List<Brand> list = brandMapper.selectByIdList(idList);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, BrandDTO.class);
    }
}
